package com.sl.au.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.sl.au.entity.ErrorStatus;
import com.sl.au.repository.ErrorStatusRepository;
import com.sl.au.service.ErrorStatusService;

@Service("ErrorStatusService")
@Transactional
public class ErrorStatusServiceImpl extends BaseServiceImpl implements ErrorStatusService {

	@Autowired
	ErrorStatusRepository errorStatusRepository;

	@Override
	public ErrorStatusRepository getRepository() {
		return this.errorStatusRepository;
	}
	
	@Override
	public List<ErrorStatus> getPage(int start, int length, String key, String orderName, Direction dir) {
		Page<ErrorStatus> pages = null;
		Pageable pageable = new PageRequest(start / length, length, dir, orderName);
		if (StringUtils.hasText(key)) {
			pages = errorStatusRepository.getByKey(key, pageable);
		} else {
			pages = errorStatusRepository.findAll(pageable);
		}
		return pages.getContent();
	}

	@Override
	public int getCount(String key) {
		int count = 0;
		if (StringUtils.hasText(key)) {
			count = errorStatusRepository.getCountByKey(key);
		} else {
			count = errorStatusRepository.getCountAll();
		}
		return count;
	}

}
